package weaver.interfaces.qz.job.util;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class AttendanceDataExtractor {
    /**
     * 从Map集合中提取考勤数据
     */
    public List<List> extractAttendanceData(Map<String, Object> kqReport, HashSet<String> monthSet) {
        List<List> attendanceList = new ArrayList<>();
        Object datasObj = kqReport.get("datas");
        if (datasObj instanceof List) {
            List<Map<String, Object>> datas = (List<Map<String, Object>>) datasObj;
            for (Map<String, Object> data : datas) {
                List<Object> attendance = extractSingleRecord(data, monthSet);
                if (attendance != null) {
                    attendanceList.add(attendance);
                }
            }
        }

        return attendanceList;
    }

    /**
     * 提取单条记录数据
     */
    private List<Object> extractSingleRecord(Map<String, Object> data, HashSet<String> monthSet) {
        ArrayList<Object> list = new ArrayList<>();
        list.add(getStringValue(data, "lastname"));
        list.add(getStringValue(data, "workcode"));
        String dyzhyggzr = getStringValue(data, "dyzhyggzr");
        String month = StringUtils.isNotEmpty(dyzhyggzr) ? dyzhyggzr.substring(0, 7) : "";
        list.add(month);
        monthSet.add(month);
        list.add(getBigDecimalValue(data, "workdays"));
        list.add(getBigDecimalValue(data, "workmins"));
        list.add(getBigDecimalValue(data, "attendancedays"));
        list.add(getBigDecimalValue(data, "attendanceMins"));
        list.add(getBigDecimalValue(data, "signmins"));
        list.add(getBigDecimalValue(data, "sjycqts"));
        list.add(getBigDecimalValue(data, "sjycqss"));
        list.add(getBigDecimalValue(data, "ypjcqsc"));
        list.add(getBigDecimalValue(data, "sjdksc"));
        list.add(getBigDecimalValue(data, "ypjdksc"));
        list.add(getIntegerValue(data, "dygzrts"));
        list.add(getIntegerValue(data, "dyfdjrts"));
        list.add(dyzhyggzr);
        list.add(getIntegerValue(data, "qyjjrts"));
        list.add(getBigDecimalValue(data, "kgtsqz"));
        list.add(getBigDecimalValue(data, "leaveType_3"));
        // 加班字段需要特殊处理
        Object overtime4leaveObj = data.get("overtime_4leave");
        if (overtime4leaveObj instanceof Map) {
            Map<String, Object> overtime4leaveMap = (Map<String, Object>) overtime4leaveObj;
            list.add(getBigDecimalValue(overtime4leaveMap, "restDayOvertime_4leave"));
        } else {
            list.add(BigDecimal.ZERO);
        }
        Object overtimeNonleaveObj = data.get("overtime_nonleave");
        if (overtimeNonleaveObj instanceof Map) {
            Map<String, Object> overtimeNonleaveMap = (Map<String, Object>) overtimeNonleaveObj;
            list.add(getBigDecimalValue(overtimeNonleaveMap, "workingDayOvertime_nonleave"));

        } else {
            list.add(BigDecimal.ZERO);
        }

        list.add(getBigDecimalValue(data, "businessLeave"));   // Integer/BigDecimal
        list.add(getBigDecimalValue(data, "officialBusiness")); // Integer/BigDecimal

        return list;
    }

    // 工具方法
    private String getStringValue(Map<String, Object> data, String fieldName) {
        Object value = data.get(fieldName);
        return value != null ? value.toString() : null;
    }

    private BigDecimal getBigDecimalValue(Map<String, Object> data, String fieldName) {
        Object value = data.get(fieldName);
        if (value != null) {
            try {
                return new BigDecimal(value.toString());
            } catch (NumberFormatException e) {
                return BigDecimal.ZERO;
            }
        }
        return BigDecimal.ZERO;
    }

    private Integer getIntegerValue(Map<String, Object> data, String fieldName) {
        Object value = data.get(fieldName);
        if (value != null) {
            try {
                return Integer.parseInt(value.toString());
            } catch (NumberFormatException e) {
                return 0;
            }
        }
        return 0;
    }
}